package com.kehutong.tag.admin;

import com.kehutong.common.entity.Root;
import com.kehutong.common.enums.Gender;
import com.kehutong.common.util.Token;
import com.kehutong.crm.enums.TagSourceType;
import com.kehutong.tag.entity.CustomizeTag;
import com.kehutong.tag.entity.UserTag;
import com.kehutong.tag.service.CustomizeTagService;
import com.kehutong.tag.service.UserTagService;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.EnumUtil;
import org.coraframework.util.Objects;
import org.coraframework.util.UUIDGenerator;

import java.io.IOException;
import java.util.List;

/**
 * 自定义标签
 *
 * @author: liukunlong
 * @date: 2021-07-28 11:52
 */
@MvcAction
@ReqMapping("/admin/crm/customize/tag")
public class CustomizeTagController {
    static final Logger logger = LoggerFactory.getLogger(CustomizeTagService.class);


    @Inject
    private JdbcSession jdbcSession;

    @Inject
    private CustomizeTagService customizeTagService;

    /**
     * 根据条件获取标签列表
     *
     * @param jsonObject 查询对象
     * @return 住户汽车列表
     * @throws IOException 操作异常
     */
    @HasPermission("crm:customize:tag:view")
    @ReqMapping("/page")
    public Page<CustomizeTag> page(JSONObject jsonObject) throws Exception {
        return customizeTagService.page(jsonObject);
    }

    @HasPermission("crm:customize:tag:view")
    @ReqMapping("/get")
    public Object get(CustomizeTag customizeTagGroup) {
        return customizeTagService.get(customizeTagGroup);
    }

    @HasPermission("crm:customize:tag:edit")
    @Bean(newInstance = true, copy = true)
    @ReqMapping("/save")
    public Object save(CustomizeTag customizeTag) {
        return customizeTagService.save(customizeTag);
    }

    @HasPermission("crm:customize:tag:edit")
    @Bean(copy = true)
    @ReqMapping("/update")
    public Object update(CustomizeTag customizeTag) throws Exception {
        return customizeTagService.update(customizeTag);
    }

    @HasPermission("crm:customize:tag:edit")
    @ReqMapping("/delete")
    public Object delete(CustomizeTag customizeTag) {
        return customizeTagService.delete(customizeTag);
    }

    /**
     * 根据条件获取标签组列表
     *
     * @param jsonObject 查询对象
     * @return 住户汽车列表
     * @throws IOException 操作异常
     */
    @HasPermission("crm:customize:tag:view")
    @ReqMapping("/group/page")
    public Page<CustomizeTag> groupPage(JSONObject jsonObject) throws Exception {
        return customizeTagService.groupPage(jsonObject);
    }

    @HasPermission("crm:customize:tag:view")
    @ReqMapping("/group/list")
    public Object groupList(JSONObject jsonObject) throws Exception {
        return customizeTagService.groupList(jsonObject);
    }

    @HasPermission("crm:customize:tag:view")
    @ReqMapping("/group/get")
    public Object groupGet(CustomizeTag customizeTagGroup) {
        return customizeTagService.groupGet(customizeTagGroup);
    }

    @HasPermission("crm:customize:tag:edit")
    @Bean(newInstance = true, copy = true)
    @ReqMapping("/group/save")
    public Object groupSave(JSONObject jsonObject, Token token) {
        String name = jsonObject.getString("name");
        JSONArray jsonArray = jsonObject.getJSONArray("childList");
        CustomizeTag customizeTagGroup = Pool.newInstance(CustomizeTag.class, UUIDGenerator.randomUUID());
        customizeTagGroup.setPid(Root.ROOT_ID);
        customizeTagGroup.setGroup(true);
        customizeTagGroup.setName(name);
        customizeTagGroup.setCreateBy(token.getUuid());
        customizeTagGroup.setCreateName(token.getUsername());
        customizeTagGroup.setSort(1);
        List<CustomizeTag> childList = JSONObject.parseArray(jsonArray.toJSONString(), CustomizeTag.class);
        customizeTagGroup.setChildList(childList);
        return customizeTagService.groupSave(customizeTagGroup);
    }

    @HasPermission("crm:customize:tag:edit")
    @Bean(copy = true)
    @ReqMapping("/group/update")
    public Object groupUpdate(JSONObject jsonObject,Token token) throws Exception {
        JSONArray jsonArray = jsonObject.getJSONArray("childList");
        if(Objects.nonEmpty(jsonArray)){
            for (Object tagJsonObj : jsonArray) {
                JSONObject tagJson = (JSONObject) tagJsonObj;
                tagJson.remove("type");
            }
        }
        jsonObject.remove("childList");
        jsonObject.remove("type");

        CustomizeTag customizeTagGroup = JSONObject.parseObject(jsonObject.toJSONString(), CustomizeTag.class);
        customizeTagGroup.setType(TagSourceType.CUSTOMIZE_TAG);
        customizeTagGroup.setCreateBy(token.getUuid());
        customizeTagGroup.setCreateName(token.getUsername());
        List<CustomizeTag> childList = JSONObject.parseArray(jsonArray.toJSONString(), CustomizeTag.class);
        customizeTagGroup.setChildList(childList);
        return customizeTagService.groupUpdate(customizeTagGroup);
    }

    @HasPermission("crm:customize:tag:edit")
    @ReqMapping("/group/delete")
    public Object groupDelete(CustomizeTag customizeTagGroup) {
        return customizeTagService.groupDelete(customizeTagGroup);
    }
}
